;
(function (window, angular) {
    'use strict';
    angular.module("Controller.BasicData.purchaseInfoRecord", [])
        .controller("PurchaseInfoRecordCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "EventBus", "UserContextService", "Upload", "ybConfig", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, EventBus, UserContextService, Upload, ybConfig) {
           
            $scope.activeFlag = [true, false];
 
            $scope.markInfo = {
                allChecked: false,
                curRow: -1,
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.condition = {};
            $scope.dataList = [];
   
            $scope.tableConfig = {
                fixedWidth: 260,
                scrollWidth: 400,
                tableCol: [
                    {
                        name: "零件号",
                        dataField: "materialNo",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "供货商代码",
                        dataField: "supplierCode",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "价格",
                        dataField: "price",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                ]
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false];
                $scope.activeFlag[index] = true;
            };

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                $scope.markInfo.curRow = -1;
                var service = "device";
                var reqUrl = "/sapPurInfoRecord/list";
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                var param = {
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                    filters: $scope.condition
                };

                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index;
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.selectAll = function () {
                angular.forEach($scope.dataList, function (item) {
                    item.isChecked = $scope.markInfo.allChecked;
                });
            };

            function checkAll() {
                var selectAllFlag = false;
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!$scope.dataList[i].isChecked) {
                        selectAllFlag = false;
                        break;
                    }
                    selectAllFlag = true;
                }
                return selectAllFlag;
            }

            $scope.selectSingle = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    $scope.markInfo.allChecked = checkAll();
                }
            };
  
            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.updateSap = function () {
                ybWidget.ShowMessage("确认从SAP下载数据吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = "/sapPurInfoRecord/syncInfo";
                    ybHttp.Get(service, reqUrl);
                    ybWidget.ShowMessage("正在后台同步，会占用较长时间，请稍候再进行查看。", MESG_TYPE.Note);
                }, function () {

                });
            };

            (function () {
                getDataList();
            })();
        }])
        ;
})(window, window.angular);
